/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.partysinfo.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.partysinfo.entity.PartysInfo;
import com.fosung.frame.modules.partysinfo.service.PartysInfoService;
import com.fosung.frame.modules.partytype.entity.PartyType;
import com.fosung.frame.modules.partytype.service.PartyTypeService;
import com.fosung.frame.modules.sys.entity.Office;
import com.fosung.frame.modules.sys.service.I85service;
import com.fosung.frame.modules.sys.service.SystemService;
import com.fosung.frame.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import net.sf.json.JSONObject;

/**
 * 组织类型信息设置Controller
 * 
 * @author wu
 * @version 2017-09-07
 */
@Controller
@RequestMapping(value = "${adminPath}/partysinfo/partysInfo")
public class PartysInfoController extends BaseController {

	@Autowired
	private PartysInfoService partysInfoService;
	@Autowired
	private PartyTypeService partyTypeService;
	@Autowired
	private I85service iservice;
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public PartysInfo get(@RequestParam(required = false) String id) {
		PartysInfo entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = partysInfoService.get(id);
		}
		if (entity == null) {
			entity = new PartysInfo();
		}
		return entity;
	}

	@RequiresPermissions("partysinfo:partysInfo:view")
	@RequestMapping(value = { "list", "" })
	public String list(PartysInfo partysInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PartysInfo> page = partysInfoService.findPage(new Page<PartysInfo>(request, response), partysInfo);
		model.addAttribute("page", page);
		return "modules/partysinfo/partysInfoList";
	}

	@RequiresPermissions("partysinfo:partysInfo:view")
	@RequestMapping(value = "form")
	public String form(PartysInfo partysInfo, Model model) {
		String ids="";
		if(partysInfo.getId()!=null&&!"".equals(partysInfo.getId())){
			List<PartysInfo> oldPartysInfo =partysInfoService.getPPartysInfo(partysInfo);
			if(oldPartysInfo!=null && oldPartysInfo.size()>0){
				for(PartysInfo info:oldPartysInfo){
					ids+=info.getOrganizationId()+",";
				}
				ids=ids.substring(0,ids.length()-1);
			}
			
			partysInfo.setOrgIds(ids);
		}
		model.addAttribute("partysInfo", partysInfo);
		PartyType pt=new PartyType();
		pt.setState("1");
		pt.setDelFlag("0");
		model.addAttribute("menuList", systemService.findAllMenu());
		List<PartyType> partyTypelist = partyTypeService.findList(pt);
		model.addAttribute("partyTypelist", partyTypelist);
		return "modules/partysinfo/partysInfoForm";
	}
	/**
	 * 	获取选择的组织的集合，
	 * 循环取出他的组织信息，新增或修改
	 * @param partysInfo
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("partysinfo:partysInfo:edit") 
	@RequestMapping(value = "save")
	public String save(PartysInfo partysInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, partysInfo)) {
			return form(partysInfo, model);
		}
		List<PartysInfo> lists=Lists.newArrayList();
		String[] strArr = partysInfo.getOrgIds().split(",");
		for (int i = 0; i < strArr.length; i++) {
			PartysInfo partysInfo1=new PartysInfo();
			Map<String, Object> plist = iservice.getOrg(strArr[i]);	//获取当前组织树信息
			partysInfo1.setOrganizationId(strArr[i]);
			partysInfo1.setOrganizationName((String) plist.get("orgName"));
			partysInfo1.setParentid(((String) plist.get("parentId")));
			partysInfo1.setCode(((String) plist.get("code")));
			partysInfo1.setTypeId(partysInfo.getTypeId());
			partysInfo1.setState(partysInfo.getState());
			boolean flag=false;
			/**
			 * 1：新增 
			 * 		判断有没有
			 */
			List<PartysInfo> oldPartysInfo =partysInfoService.checkByOrganizationId(partysInfo1);
			if(oldPartysInfo!=null && oldPartysInfo.size()>0){//有值
				if(partysInfo1.getId()!=null&&!"".equals(partysInfo1.getId())){//修改
				}else{//新增
					flag=true;
				}
			}
			if(flag){
				addMessage(model, "该组织"+partysInfo1.getOrganizationName()+"已经选择了组织类型信息！");
				return form(partysInfo, model);
			}
			lists.add(partysInfo1);
		}
		partysInfoService.save(lists);
		addMessage(redirectAttributes, "保存组织类型信息设置成功");
		return form(partysInfo, model);
	}

	@RequiresPermissions("partysinfo:partysInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(PartysInfo partysInfo, RedirectAttributes redirectAttributes) {
		partysInfoService.delete(partysInfo);
		addMessage(redirectAttributes, "删除组织类型信息设置成功");
		return "redirect:" + Global.getAdminPath() + "/partysinfo/partysInfo/?repage";
	}

	@RequiresPermissions("partysinfo:partysInfo:edit")
	@RequestMapping(value = "updateStart")
	public String updateStart(PartysInfo partysInfo, RedirectAttributes redirectAttributes) {
		partysInfoService.updateStart(partysInfo);
		addMessage(redirectAttributes, "启动组织类型信息成功");
		return "redirect:" + Global.getAdminPath() + "/partysinfo/partysInfo/?repage";
	}

	@RequiresPermissions("partysinfo:partysInfo:edit")
	@RequestMapping(value = "updateStop")
	public String updateStop(PartysInfo partysInfo, RedirectAttributes redirectAttributes) {
		partysInfoService.updateStop(partysInfo);
		addMessage(redirectAttributes, "关闭组织类型信息成功");
		return "redirect:" + Global.getAdminPath() + "/partysinfo/partysInfo/?repage";
	}

	@ResponseBody
	@RequestMapping(value = "checkName")
	public String checkName(String oldName, String organizationId) {
		PartysInfo partysInfo = new PartysInfo();
		partysInfo.setOrganizationId(organizationId);
		if (organizationId != null && organizationId.equals(oldName)) {
			return "true";
		} else if (organizationId != null && partysInfoService.check(partysInfo) == null) {
			return "true";
		}
		return "false";
	}
	
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String organizationId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Map<String, Object>> plist =Lists.newArrayList();
		Office office =UserUtils.getUser().getOffice();
		if(organizationId !=null&&!"".equals(organizationId)){
			plist = iservice.queryOrg(organizationId);	
		}else{
			plist = iservice.queryOrg(office.getId());	
		}
		if(plist.size()>0){
			for (int i = 0; i < plist.size(); i++) {  
				Map<String, Object> e = plist.get(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.get("orgId"));
				map.put("pId", e.get("parentId"));
				map.put("name", StringUtils.replace((String)e.get("orgName"), " ", ""));
				mapList.add(map);
			}
		}
		return mapList;
	}
	
	@RequiresPermissions("partysinfo:partysInfo:view")
	@RequestMapping(value = "peizhi")
	public String peizhi(PartysInfo partysInfo, Model model) {
		model.addAttribute("partysInfo", partysInfo);
		if(partysInfo.getpOrganizationId()!=null&&!"".equals(partysInfo.getpOrganizationId())){
			model.addAttribute("organizationId",partysInfo.getParentid());
		}else{
			model.addAttribute("organizationId",partysInfo.getOrganizationId());	
		}
		PartyType pt=new PartyType();
		pt.setState("1");
		pt.setDelFlag("0");
		List<PartyType> partyTypelist = partyTypeService.findList(pt);
		model.addAttribute("partyTypelist", partyTypelist);
		return "modules/partysinfo/peizhi"; 
	}
	
	@RequiresPermissions("partysinfo:partysInfo:edit") 
	@RequestMapping(value = "savePz")
	public String savePz(PartysInfo partysInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, partysInfo)) {
			return peizhi(partysInfo, model);
		}
		Map<String, Object> plist = iservice.getOrg(partysInfo.getOrganizationId());	
		partysInfo.setOrganizationName((String) plist.get("orgName"));
		partysInfo.setParentid(((String) plist.get("parentId")));
		partysInfo.setCode(((String) plist.get("code")));
		/**
		 * 1：新增 
		 * 		判断有没有
		 * 	2：修改
		 * 		判断是不是同一个
		 * 		
		 */
		boolean flag=false;
		List<PartysInfo> oldPartysInfo =partysInfoService.checkByOrganizationId(partysInfo);
		if(oldPartysInfo!=null && oldPartysInfo.size()>0){//有值
			if(partysInfo.getId()!=null&&!"".equals(partysInfo.getId())){//修改
			}else{//新增
				flag=true;
			}
		}
		if(flag){
			addMessage(model, "该组织已经选择了组织类型信息！");
			return peizhi(partysInfo, model);
		}
		partysInfoService.save(partysInfo);
		partysInfo.setpOrganizationId(partysInfo.getOrganizationId());
		addMessage(redirectAttributes, "保存组织类型信息设置成功");
		return peizhi(partysInfo, model);
	}
	/**
	 * 	异步加载获取组织树信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/gettreeData")
	public @ResponseBody Object gettreeData(HttpServletRequest request, HttpServletResponse response) {
		String organizationId = request.getParameter("id");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Map<String, Object>> plist =Lists.newArrayList();
		Office office =UserUtils.getUser().getOffice();
		if(organizationId !=null&&!"".equals(organizationId)){
			plist = iservice.queryOrg(organizationId);	
			plist.remove(0);
		}else{
			plist = iservice.queryOrg(office.getId());	
		}
		if(plist!=null&&plist.size()>0){
			for (int i = 0; i < plist.size(); i++) {  
				Map<String, Object> e = plist.get(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.get("orgId"));
				map.put("pId", e.get("parentId"));
				map.put("name", StringUtils.replace((String)e.get("orgName"), " ", ""));
				map.put("hasChildren", e.get("hasChildren"));
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}